package com.ezgo.erc.master.utils;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import net.sf.json.xml.XMLSerializer;

/**
 * java对象与xml互转的util工具包
 * 
 * @author cl232611
 *
 */
public class XmlUtil {

	private XmlUtil() {
	}

	/**
	 * java对象转xml
	 * 
	 * @param obj
	 *            被转换的java对象
	 * @param sw
	 *            流StringWriter
	 * @param clazz
	 *            被转换的java对象的类对象
	 * @return xml格式的数据
	 * @throws Exception
	 */
	public static String toXml(Object obj, Class<?> clazz) throws Exception {
		try {
			StringWriter sw = new StringWriter();
			JAXBContext context = JAXBContext.newInstance(clazz);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(obj, sw);
			return sw.toString();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * xml转java对象
	 * 
	 * @param xmlstr
	 *            被转换的xml
	 * @param clazz
	 *            java对象的类对象
	 * @return 对应的java对象
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toJava(String xmlstr, Class<?> clazz) throws Exception {
		try {
			JAXBContext context = JAXBContext.newInstance(clazz);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			return (T) unmarshaller.unmarshal(new StringReader(xmlstr));
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	// ===================================处理Xml文件的部分========================================
	/**
	 * 读取xml文件生成java对象
	 * 
	 * @param clazz
	 *            java对象的类对象
	 * @param xmlStream
	 *            读取文件的I/O流 e.g. InputStream is = new
	 *            FileInputStream("C:/test.xml");
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> T ToJava(Class<?> clazz, InputStream xmlStream)
			throws Exception {
		try {
			JAXBContext context = JAXBContext.newInstance(clazz);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			return (T) unmarshaller.unmarshal(xmlStream);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * java对象输出生成xml文件
	 * 
	 * @param obj
	 * @param clazz
	 *            java对象的类对象
	 * @param stream
	 *            生成文件的I/O流 e.g. OutputStream os = new
	 *            FileOutputStream("C:/test.xml");
	 * @throws Exception
	 */
	public static void ToXml(Object obj, Class<?> clazz, OutputStream stream)
			throws Exception {
		try {
			JAXBContext context = JAXBContext.newInstance(clazz);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(obj, stream);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * Xml转化JSON格式
	 * 
	 * @param xmlString
	 *            xml字符串
	 * @return
	 */
	public static String xml2Json(String xmlString) {
		XMLSerializer xmlSerializer = new XMLSerializer();
		return xmlSerializer.read(xmlString).toString();
	}

}
